package tt.dz.order.pojo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tt.dz.order.dao.BaseDAOUtil;
import tt.dz.order.exception.BusinessException;

/**
 * @Title: 订单系统菜单信息,对应order_menu表
 * @author zhoutian   
 * @date 2015-12-24
 */
public class SysMenu {
	//菜单ID
	private String menuId;
	//父级菜单ID
	private String parentMenuId;
	//菜单路径
	private String url;
	//菜单名称
	private String menuName;
	//功能描述
	private String menuDesc;
	//排序号
	private int sort;
	//有效标识,0:无效 1:有效
	private int isValid;
	//创建人
	private String creator;
	//创建时间
	private Date createTime;

	public String getMenuId() {
		return menuId;
	}

	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}

	public String getParentMenuId() {
		return parentMenuId;
	}

	public void setParentMenuId(String parentMenuId) {
		this.parentMenuId = parentMenuId;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getMenuName() {
		return menuName;
	}

	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}

	public String getMenuDesc() {
		return menuDesc;
	}

	public void setMenuDesc(String menuDesc) {
		this.menuDesc = menuDesc;
	}

	public int getSort() {
		return sort;
	}

	public void setSort(int sort) {
		this.sort = sort;
	}

	public int getIsValid() {
		return isValid;
	}

	public void setIsValid(int isValid) {
		this.isValid = isValid;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public SysMenu() {}
	
	private static final String select_all_sql = "select menu_id as menuId, parent_menu_id as parentMenuId, url, menu_name as menuName, menu_desc as menuDesc, sort, is_valid as isValid, creator, createTime from order_menu";
	
	private static final String update_all_sql = "update order_menu set menu_id = :menuId, parent_menu_id = :parentMenuId, url = :url, menu_name = :menuName, menu_desc = :menuDesc, sort = :sort, is_valid = :isValid, creator = :creator, createTime = :createTime";
	
	public void save() throws BusinessException{
		if(isMenuNameExist(this.parentMenuId, "", this.menuName)){
			throw new BusinessException("同级菜单名称不能重复");
		}
		String sql = "insert into order_menu(menu_id, parent_menu_id, url, menu_name, menu_desc, sort, is_valid, creator, createTime) values(:menuId, :parentMenuId, :url, :menuName, :menuDesc, :sort, :isValid, :creator, :createTime)";
		BaseDAOUtil.updateObject(sql, this);
	}
	
	public void update() throws BusinessException{
		if(isMenuNameExist(this.parentMenuId, this.menuId, this.menuName)){
			throw new BusinessException("同级菜单名称不能重复");
		}
		String sql = update_all_sql + " where menu_id = :menuId";
		BaseDAOUtil.updateObject(sql, this);
	}
	
	public void delete(){
		String sql = "delete from order_menu where menu_id = :menu_id";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("menu_id", this.menuId);
		BaseDAOUtil.delete(sql, paramMap);
	}
	
	public static boolean isMenuNameExist(String parent_menu_id, String menu_id, String menu_name){
		String sql = select_all_sql + " where parent_menu_id = :parent_menu_id and menu_id != :menu_id and menu_name = :menu_name";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("parent_menu_id", parent_menu_id);
		paramMap.put("menu_id", menu_id);
		paramMap.put("menu_name", menu_name);
		List<SysMenu> list = BaseDAOUtil.queryForList(sql, paramMap, SysMenu.class);
		if(list != null && list.size() > 0){
			return true;
		}
		return false;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static List<SysMenu> getAll(){
		String sql = select_all_sql + " order by sort asc";
		return BaseDAOUtil.queryForList(sql, new HashMap(), SysMenu.class);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static List<SysMenu> getAllValid(){
		String sql = select_all_sql + " where is_valid = 1 order by sort asc";
		return BaseDAOUtil.queryForList(sql, new HashMap(), SysMenu.class);
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static SysMenu getBy(String menuId){
		String sql = select_all_sql + " where menu_id = :menuId";
		Map paramMap = new HashMap();
		paramMap.put("menuId", menuId);
		return BaseDAOUtil.queryForObject(sql, paramMap, SysMenu.class);
	}
	
	public static Integer getMaxSeqNo(){
		String sql = "select menu_id from order_menu order by cast (menu_id as numeric) desc";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		List<Map<String, Object>> menuIdList = BaseDAOUtil.queryForList(sql, paramMap);
		if(menuIdList == null || menuIdList.size() == 0){
			return 0;
		}
		Map<String, Object> resultMap = menuIdList.get(0);
		return (Integer) Integer.valueOf((String)resultMap.get("menu_id"));
	}

}
